<link rel="stylesheet" href="./wsc.css">
<!-- 倒计时 -->
<div class="thelast">
    倒计时:
</div>
<!-- 开始 -->
<div class="start">
    <p>分数</p>
    <button>replay</button>
</div>
<!-- 分数 -->
<div class="data">0</div>
<!-- 王思聪 -->
<div class="wsc">
    <img src="./images/sicong.jpg" alt="">
</div>
<script>
//禁止选中图片
if(document.all){
       document.onselectstart= function(){return false;}; //for ie
  }else{
       document.onmousedown= function(){return false;};
       document.onmouseup= function(){return true;};
 }
 document.onselectstart = new Function('event.returnValue=false;');
 //王思聪按下移动
 let wscObj=document.querySelector('.wsc')
 wscObj.onmouseover=function(){
     document.onmousemove=function(evt){
         var e =evt||window.event
         var left =e.clientX
         if(left<0) {
             left=0
            }
        var maxleft=(window.innerWidth||document.documentElement.clientWidth)-wscObj.offsetWidth
         if(left>maxleft) {
            left=maxleft
         }
         left =left-wscObj.offsetWidth/2
         wscObj.style.left=left+'px'
     }
    //  document.onmouseup=function(){
    //     document.onmousemove=null
    //  }
 }
 //倒计时事件
 var lastObj=document.querySelector('.thelast')
 var startObj=document.querySelector('.start')
 let startpObj=document.querySelector('.start p')
 var n =10
 window.onload=function(){
     data=0
     createhotdogObj()
    //  eathotdogObj()
    //  diaohotdong()
     t=setInterval(function(){
        n--
        lastObj.innerText='倒计时:' +n+'S'
        if(n==0){
            clearInterval(t)
            wscObj.onmousedown='null'
            startObj.style.display='block'
            startpObj.innerHTML='分数： '+data
            // console.log(n);
            clearInterval(cxhotdog)
            clearTimeout(downhotdog)
            clearInterval(eatt)
            document.onmousemove=null
            // console.log(eat);
            hotdogarr=[]
            n=10
            return
        }
     },1000)
 }
 //重新开始计时事件
 var stratBtnObj=document.querySelector('.start button')
 let dataObj=document.querySelector('.data')
 stratBtnObj.onclick=function(){
     data=0
     startObj.style.display='none'
     dataObj.innerText='0'
     createhotdogObj()
    //  eathotdogObj()
    //  diaohotdong()
     t=setInterval(function(){
         n-=1
         lastObj.innerText='倒计时:' +n+'S'
         if(n==0){
             clearInterval(t)
             wscObj.onmousedown='null'
             startObj.style.display='block'
             startpObj.innerHTML='分数： '+data
             clearInterval(cxhotdog)
             clearTimeout(downhotdog)
             clearInterval(eatt)
             hotdogarr=[]
             n=10
            return
            }
        },1000)
    }
    //制造王思聪吃的热狗
    let hotdogarr=[]
    var createhotdogObj=function(){
        cxhotdog=setInterval(function(){
            hotdogObj=document.createElement('img')
            hotdogObj.src='./images/hotdog.jpg'
            hotdogObj.className='hotdogObj'
            hotdogObj.width='30'
            hotdogObj.height='50'
            hotdogObj.style.position='absolute'
            var maxleft = (window.innerWidth ||window.document.documentElement.clientWidth)-hotdogObj.width
            // var maxtop = (window.innerHeight||window.document.documentElement.clientHeight)-width
            // hotdogObj.style.top=Math.floor(Math.random()*(maxtop-0+1)+0)
            hotdogObj.style.top=-100+'px'
            // console.log(hotdogObj.style.top);
            hotdogObj.style.left=Math.floor(Math.random()*(maxleft-0+1)+0)
            // console.log(hotdogObj.width,hotdogObj.height,hotdogObj.style.left,hotdogObj.style.top);
            document.body.appendChild(hotdogObj)
            // console.log(hotdogObj);
            hotdogarr.push(hotdogObj.style.left)

            downhotdog=setTimeout(function(){
                hotdogObj.style.top=2000+'px'
                eathotdogObj()
        },100)
        // console.log(hotdogarr);
    },2000)
}
// var diaohotdong=function(){
//     downhotdog=setInterval(function(){
//         downhotdog2=setTimeout(function(){
//             hotdogObj.style.top=2000+'px'
//         },10)
//     },400)
// }
let data=0
//封装吃热狗的函数
function eathotdogObj(){
   eatt=setInterval(function(){
    //    console.log(hotdogObj.offsetTop,window.innerHeight,0-(wscObj.offsetWidth),wscObj.offsetWidth);
       console.log(hotdogObj.offsetTop,window.innerHeight);
       setInterval(() => {
        if (hotdogObj.offsetTop>=672&&hotdogObj.offsetTop<=722) {
               console.log(1);
               var wsc=wscObj.offsetLeft-hotdogObj.offsetLeft
               console.log(wsc);
                if (wsc>= -30&&wsc<=30) {
                    console.log(2);
                    clearInterval(eatt)
                    data+=1
                    dataObj.innerHTML=data
                    document.body.removeChild(hotdogObj)
                }
           }
       }, 10);
           
   },1)
}
</script>